/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.filters;

import java.io.IOException;
import java.util.Map;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;


public class LengthTokenFilter extends ElasticFilter{

	public static final String MIN_PARAM_KEY = "min";
	public static final String MAX_PARAM_KEY = "max";
	
	private Integer min;
	private Integer max;
	
	
	public LengthTokenFilter(String defName){
		this(null,defName);
	}
	
	public LengthTokenFilter(Map<String,String> params, String defName){
		super(ElasticFilterType.length, defName);
		if(params!=null){
			String minStr = params.get(MIN_PARAM_KEY);
			if(minStr!=null&&!minStr.isEmpty()){
				try {
					min = Integer.parseInt(minStr);
				} catch (NumberFormatException e) {
				}
			}
			
			String maxStr = params.get(MAX_PARAM_KEY);
			if(maxStr!=null&&!maxStr.isEmpty()){
				try {
					max = Integer.parseInt(maxStr);
				} catch (NumberFormatException e) {
				}
			}
		}
	}

	@Override
	public boolean hasCustomParams() {
		if(min!=null || max!=null)return true;
		return false;
	}

	@Override
	public XContentBuilder addToBuilder(
			XContentBuilder settingsBuilder) throws IOException {
		settingsBuilder.startObject(getDefinitionName())
		.field("type", getComponentSubTypeName());
		
		if(min!=null){
			settingsBuilder.field(MIN_PARAM_KEY, min.intValue());
		}
		if(max!=null){
			settingsBuilder.field(MAX_PARAM_KEY, max.intValue());
		}
		return settingsBuilder;
	}

	
	
	
	
	@Override
	public void setParamsFromSettings(Settings settings) {

		String minSetting = settings.get(MIN_PARAM_KEY);
		if(minSetting!=null && !minSetting.isEmpty()){
			try {
				this.min = Integer.parseInt(minSetting);
			} catch (NumberFormatException e) {
			}
		}
		String maxSetting = settings.get(MAX_PARAM_KEY);
		if(maxSetting!=null && !maxSetting.isEmpty()){
			try {
				this.max = Integer.parseInt(maxSetting);
			} catch (NumberFormatException e) {
			}
		}		
	}

	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition other) {
		if(other == null)return false;
		LengthTokenFilter o = null;
		try {
			o = (LengthTokenFilter) other;
		} catch (Exception e) {
			return false;
		}
		return 
		EqualsUtil.areEqual(this.min, o.min)
		&&
		EqualsUtil.areEqual(this.max, o.max);
	}
	
	
	
	
	
	
	
	
	
}
